package online.flowerinsnow.minecraft.bukkit.testffa.manager;

import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;

public class EnderPearlCoolDownManagerImpl implements IEnderPearlCoolDownManager {
    // 根据时间戳来记录玩家剩余末影珍珠冷却时间
    @NotNull private Map<Player, Long> enderPearlCoolDown = new HashMap<>();
    @Override
    public long getPlayerEnderPearlCoolDown(@NotNull Player player) {
        if (!player.isOnline()) return -1L;
        // 用玩家冷却到期的时间戳(不存在为0) 减去现在的时间戳 获得玩家还剩多少毫秒冷却 然后判断是否比0小 若比0小 返回0
        return Math.max(0, enderPearlCoolDown.getOrDefault(player, 0L) - System.currentTimeMillis());
    }

    @Override
    public void setPlayerEnderPearlCoolDown(@NotNull Player player, long coolDown) {
        if (coolDown <= 0L) enderPearlCoolDown.remove(player);
        // 用现在的时间加上冷却时间 获取冷却时间到期时间
        enderPearlCoolDown.put(player, System.currentTimeMillis() + coolDown);
    }
}
